package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.BulewallRequest;
import com.jiyinit.web.core.business.dto.NoticeRequest;
import com.jiyinit.web.core.business.dto.UserTaskRequest;
import com.jiyinit.web.core.business.entity.TabCoreTask;
import com.jiyinit.web.core.business.service.BulewallServiceImpl;
import com.jiyinit.web.core.business.service.UserTaskServiceImpl;
import com.jiyinit.web.core.business.vo.NoticeUserVo;
import com.jiyinit.web.core.business.vo.UserTaskVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lr on 2018/1/2.
 */
@Controller
@RequestMapping("mobile/bulewall")
public class MbuleWallController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MbuleWallController.class);

    @Resource(name = "bulewallServiceImpl")
    private BulewallServiceImpl bulewallService;

    @Resource(name = "core.userTaskServiceImpl")
    private UserTaskServiceImpl userTaskService;

    @ResponseBody
    @RequestMapping("add")
    public ResponseEntity<ResponseDto> add(HttpServletRequest request){
        BulewallRequest bulewallRequest = getJsonToTaskRequest(request);
        if (bulewallRequest.getContent() == null) {
            return new ResponseEntity<>(ResponseDto.responseFail("内容不能为空"), HttpStatus.OK);
        }
        if (bulewallRequest.getTitle() == null) {
            return new ResponseEntity<>(ResponseDto.responseFail("标题不能为空"), HttpStatus.OK);
        }
        if (bulewallRequest.getChannel() == null) {
            return new ResponseEntity<>(ResponseDto.responseFail("渠道不能为空"), HttpStatus.OK);
        }
        int num = bulewallService.add(bulewallRequest);
        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("新建失败！"), HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("delete")
    public ResponseEntity<ResponseDto> delete(HttpServletRequest request){
        BulewallRequest bulewallRequest = getJsonToTaskRequest(request);
        if (bulewallRequest.getId() == null) {
            return new ResponseEntity<>(ResponseDto.responseFail("ID不能为空"), HttpStatus.OK);
        }
        TabCoreTask bulewall = bulewallService.detail(bulewallRequest.getId());
        if(bulewall == null)
            return new ResponseEntity<>( ResponseDto.responseFail("该蓝墙不存在！"), HttpStatus.OK);
        if(bulewall.getCreateUid().intValue() != bulewallRequest.getUserId().intValue()){
            return new ResponseEntity<>( ResponseDto.responseFail("没有权限删除！"), HttpStatus.OK);
        }

        int num = bulewallService.delete(bulewallRequest);
        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("删除失败！"), HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("detail")
    public ResponseEntity<ResponseDto> detail(HttpServletRequest request){
        BulewallRequest bulewallRequest = getJsonToTaskRequest(request);
        if (bulewallRequest.getId() == null) {
            return new ResponseEntity<>(ResponseDto.responseFail("ID不能为空"), HttpStatus.OK);
        }
        TabCoreTask bulewall = bulewallService.detail(bulewallRequest.getId());
        UserTaskRequest uses = new UserTaskRequest();
        uses.setTaskId(bulewallRequest.getId());
        List<UserTaskVo> users = userTaskService.finAll(uses);
        HashMap<String,Object> result =  new HashMap<String,Object>();
        result.put("bulewall",bulewall);
        result.put("alloters",users);
        if(bulewall != null)
            return new ResponseEntity<>( ResponseDto.responseOK(result), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("查询失败！"), HttpStatus.OK);
    }

    private BulewallRequest getJsonToTaskRequest(HttpServletRequest request){
        String content = (String) request.getAttribute("request_json");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        BulewallRequest bulewallRequest = (BulewallRequest) JsonEntityTransform.json2Object(logJson, BulewallRequest.class);
        return bulewallRequest;
    }
}
